package com.eduService.teacher.controller;

import com.eduService.teacher.Client.VideoClient;
import com.eduService.teacher.entity.EduVideo;
import com.eduService.teacher.service.IEduVideoService;
import com.eduService.teacher.utils.R;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-05-01
 */
@CrossOrigin
@RestController
@RequestMapping("/eduvideo")
public class EduVideoController {

    @Autowired
    private VideoClient videoClient;

    @Autowired
    IEduVideoService iEduVideoService;

    /*
    * 添加小节
    * */
    @PostMapping("savevideo")
    public R savevideo(@RequestBody EduVideo eduVideo){
        boolean save = iEduVideoService.save(eduVideo);
        if (save){
           return R.ok().msg("成功添加章节小节");
        }
        else{
            return  R.error().msg("添加失败！");
        }
    }

    /*
    * 删除小节
    * */
    @DeleteMapping("delVideo/{id}")
    public R delVideo(@PathVariable("id") String id){
//        根据ID找到视频ID
        EduVideo byId = iEduVideoService.getById(id);
        String videoSourceId = byId.getVideoSourceId();
        if(videoSourceId == null){
           iEduVideoService.removeById(id);
            return R.ok().msg("成功删除小节！");
        }else{
            videoClient.delVideo(videoSourceId);
            iEduVideoService.removeById(id);
            return R.ok().msg("成功删除小节与相应视频！");
        }

    }




}
